from game.betting import Betting
from game.config import small_blind, big_blind
from game.deck import Deck
from game.hand_evaluator import HandEvaluator


class GameManager:
    def __init__(self, players):
        self.players = players
        self.deck = Deck()
        self.betting = Betting(small_blind, big_blind)
        self.num_players = len(players)
        self.community_cards = []
        self.dealer_position = 0

    def deal_cards(self):
        for player in self.players:
            player.receive_cards(self.deck.deal(2))
            print(f"给玩家 {player.name} 发牌: {player.hole_cards}")

    def play_flop(self):
        self.community_cards = self.deck.deal(3)
        print(f"翻牌: {self.community_cards}")

    def play_turn(self):
        self.community_cards.extend(self.deck.deal(1))
        print(f"转牌: {self.community_cards[-1]}")

    def play_river(self):
        self.community_cards.extend(self.deck.deal(1))
        print(f"河牌: {self.community_cards[-1]}")

    def rotate_dealer(self):
        """每轮游戏后旋转庄家位置"""
        self.dealer_position = (self.dealer_position + 1) % self.num_players  # 每轮游戏后旋转庄家位置

    def showdown(self):
        best_hand = None  # 用于记录当前最好的手牌信息
        winner = None  # 用于记录当前的赢家

        for player in self.players:
            # 将玩家的手牌和公共牌组合成完整的牌
            full_hand = player.hole_cards + self.community_cards
            evaluator = HandEvaluator(full_hand)

            # 获取当前玩家的手牌信息
            hand_rank, best_five_cards, score = evaluator.get_hand_rank()
            print(f'\n玩家：{player.name},\n 牌型: {hand_rank},最佳五张牌: {best_five_cards},牌型分数: {score}')

            # 如果当前没有最好的手牌，则将当前玩家的手牌设为最好的手牌
            if best_hand is None:
                best_hand = (player.name, hand_rank, best_five_cards, score)
                winner = player
            # 如果当前玩家的手牌得分高于目前最好的手牌得分，则更新最好的手牌和赢家
            if score > best_hand[3]:
                best_hand = (player.name, hand_rank, best_five_cards, score)
                winner = player
            # 如果当前玩家的手牌得分与目前最好的手牌得分相同，则比较两手牌
            elif score == best_hand[3]:
                # 调用 HandEvaluator.compare_hands 方法比较两手牌
                comparison_result = HandEvaluator.compare_hands(best_five_cards, best_hand[2])
                if comparison_result > 0:
                    best_hand = (player.name, hand_rank, best_five_cards, score)
                    winner = player

        # 打印赢家信息
        print(f'\n赢家是: {winner.name}, 牌型: {best_hand[1]}, 最佳五张牌: {best_hand[2]}')
        return winner  # 返回赢家

    def start_game(self):
        self.deck.shuffle()
        self.deal_cards()
        self.betting.place_blinds(self.players)

        # 下注轮次
        starting_player = (self.dealer_position + 3) % len(self.players)  # 大盲注之后的玩家
        self.betting.round(self.players, starting_player)
        print(f'当前底池：{self.betting.pot}')

        # 公共牌翻牌圈
        self.play_flop()
        self.betting.round(self.players, starting_player)

        print(f'当前底池：{self.betting.pot}')

        # 公共牌转牌圈
        self.play_turn()
        self.betting.round(self.players, starting_player)
        print(f'当前底池：{self.betting.pot}')

        # 公共牌河牌圈
        self.play_river()
        self.betting.round(self.players, starting_player)
        print(f'当前底池：{self.betting.pot}')

        # 摊牌
        winner = self.showdown()
        self.betting.resolve_bets(winner)

        for player in self.players:
            print(f"玩家 {player.name}，他现在有 {player.chips} 筹码。")
        print(f"赢家是玩家 {winner.name}，他现在有 {winner.chips} 筹码。")

        # 重置游戏状态，准备下一局
        self.reset_game()

        return winner

    def reset_game(self):
        """
        重置游戏状态，准备下一局
        """
        self.deck = Deck()
        self.community_cards = []
        self.betting.reset()
        self.dealer_position = (self.dealer_position + 1) % len(self.players)
        for player in self.players:
            player.reset_for_new_round()
